package com.zhumr.serviceImpl;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhumr.dao.UserMapper;
import com.zhumr.model.User;
import com.zhumr.service.IUserService;

@Service("userService")
public class UserServiceImpl implements IUserService {
	private static Logger logger = Logger.getLogger(UserServiceImpl.class);

	@Autowired
	private UserMapper userMapper;

	/**
	 * 登录
	 */
	public Map<String, Object> selectUserByName(User user, String password) {

		Map<String, Object> map = new HashMap<String, Object>();

		User resultUser = userMapper.selectUserByName(user);

		logger.info(resultUser);

		if (resultUser != null) {
			if (resultUser.getPasswordN().equals(password)) {
				map.put("code", "0");
				map.put("user", resultUser);
			} else {
				// 密码错误
				map.put("code", "2");
			}
		} else {
			// 账号错误
			map.put("code", "1");
		}
		return map;
	}

	/**
	 * 注册
	 * 
	 * @param user
	 * @return
	 */
	public Map<String, Object> registered(User user) {
		int i = userMapper.registered(user);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (i > 0) {
			resultMap.put("code", "0");
		} else {
			resultMap.put("code", "1");
		}

		return resultMap;
	}

	/**
	 * 重置密码
	 */
	public Map<String, Object> resetPass(User user) {

		int i = userMapper.resetPass(user);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (i > 0) {
			resultMap.put("code", "0");
		} else {
			resultMap.put("code", "1");
		}

		return resultMap;
	}
}
